{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "from joblib import Parallel, delayed\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_path = '../data/testData0626.csv'\n",
    "route_order_folder_path = '../data/route_order_data_several'\n",
    "port_path = '../data/port.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['CNSHK-CLVAP',\n",
       " 'CNYTN-ARENA',\n",
       " 'CNSHK-GRPIR',\n",
       " 'CNHKG-ARBUE',\n",
       " 'CNNSA-GHTEM',\n",
       " 'CNNSA-NAWVB',\n",
       " 'HKHKG-FRFOS',\n",
       " 'HONGKONG-BU']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def format_data_type(data, mode='train'):\n",
    "    if mode=='test':\n",
    "        data['onboardDate'] = pd.to_datetime(data['onboardDate'], infer_datetime_format=True)\n",
    "        data['temp_timestamp'] = data['timestamp']\n",
    "        data['ETA'] = None\n",
    "        data['creatDate'] = None\n",
    "    data['loadingOrder'] = data['loadingOrder'].astype(str)\n",
    "    data['timestamp'] = pd.to_datetime(data['timestamp'], infer_datetime_format=True)\n",
    "    data['longitude'] = data['longitude'].astype(float)\n",
    "    data['latitude'] = data['latitude'].astype(float)\n",
    "    data['speed'] = data['speed'].astype(float)\n",
    "    data['TRANSPORT_TRACE'] = data['TRANSPORT_TRACE'].astype(str)\n",
    "    return data\n",
    "\n",
    "def get_test_data_info(path):\n",
    "    data = pd.read_csv(path) \n",
    "#     test_trace_set = data['TRANSPORT_TRACE'].unique()\n",
    "    test_trace_set = ['CNSHK-CLVAP','CNYTN-ARENA','CNSHK-GRPIR','CNHKG-ARBUE',\n",
    "                    'CNNSA-GHTEM','CNNSA-NAWVB','HKHKG-FRFOS','HONGKONG-BU']\n",
    "    test_order_belong_to_trace = {}\n",
    "    for item in test_trace_set:\n",
    "        orders = data[data['TRANSPORT_TRACE'] == item]['loadingOrder'].unique()\n",
    "        test_order_belong_to_trace[item] = orders\n",
    "    return format_data_type(data, mode='test'), test_trace_set, test_order_belong_to_trace\n",
    "\n",
    "test_data_origin, test_trace_set, test_order_belong_to_trace = get_test_data_info(test_data_path)\n",
    "\n",
    "def get_port_info():\n",
    "    port_data = {}\n",
    "    test_port_set = set()\n",
    "    for route in test_trace_set:\n",
    "        ports = route.split('-')\n",
    "        test_port_set = set.union(test_port_set, set(ports))\n",
    "    port_data_origin = pd.read_csv(port_path)\n",
    "    for item in port_data_origin.itertuples():\n",
    "        if getattr(item, 'TRANS_NODE_NAME') in test_port_set:\n",
    "            port_data[getattr(item, 'TRANS_NODE_NAME')] = {'LONGITUDE': getattr(item, 'LONGITUDE'),'LATITUDE': getattr(item, 'LATITUDE') }\n",
    "    return port_data\n",
    "port_data = get_port_info()\n",
    "test_trace_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import radians, cos, sin, asin, sqrt\n",
    "def haversine(lon1, lat1, lon2, lat2): # 经度1，纬度1，经度2，纬度2 （十进制度数）\n",
    "    # 将十进制度数转化为弧度\n",
    "    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])\n",
    "    # haversine公式\n",
    "    dlon = lon2 - lon1 \n",
    "    dlat = lat2 - lat1 \n",
    "    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2\n",
    "    c = 2 * asin(sqrt(a)) \n",
    "    r = 6371 # 地球平均半径，单位为公里\n",
    "    return c * r * 1000\n",
    "def get_train_route_order_data(route):\n",
    "    route_order_data_path = os.path.join(route_order_folder_path, \"{}.csv\".format(route))\n",
    "    data = pd.read_csv(route_order_data_path, header=None\n",
    "           , names=['loadingOrder','timestamp','longitude','latitude','speed'])\n",
    "    if (data.shape[0] == 0):\n",
    "        print(\"error == \", route)\n",
    "    data['timestamp'] = pd.to_datetime(data['timestamp'], infer_datetime_format=True)\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def handle_train_data(order, order_info_set,start_longitude,start_latitude,dest_longitude,dest_latitude):\n",
    "    order_info_set = order_info_set.reset_index(drop=True)\n",
    "    #       获取起航时间\n",
    "    start_time = order_info_set['timestamp'].min()\n",
    "    start_index = 0\n",
    "    for (index, info_item) in order_info_set.iterrows():\n",
    "        if abs(info_item['longitude']-start_longitude) < 2 and abs(info_item['latitude']-start_latitude) < 2 and info_item['speed'] > 0:\n",
    "            start_time = max(start_time, info_item['timestamp'])\n",
    "            start_index = index\n",
    "            break \n",
    "#       获取到达目的地时间，这里需要用 GPS 判断\n",
    "    end_time = order_info_set['timestamp'].max()\n",
    "    end_index = order_info_set.size-1\n",
    "    for (index, info_item) in order_info_set.iterrows():\n",
    "        if abs(info_item['longitude'] - dest_longitude) < 2 and abs(info_item['latitude'] - dest_latitude) < 2:\n",
    "            end_time = min(end_time, info_item['timestamp'])\n",
    "            end_index = index\n",
    "            break\n",
    "#         修正起点终点逆序\n",
    "    if (end_time < start_time):\n",
    "        start_time,end_time = end_time,start_time\n",
    "        start_index,end_index = end_index,start_index\n",
    "#         算出航行用时\n",
    "    total_seconds = (end_time - start_time).total_seconds()\n",
    "#         截取数据\n",
    "    order_info_set = order_info_set[start_index:end_index+1]\n",
    "    if (order_info_set.shape[0] > 100):\n",
    "        index = np.linspace(0, order_info_set.shape[0]-1, num=100,dtype=int).tolist()\n",
    "        order_info_set = order_info_set.iloc[index]     \n",
    "#         得到速度中位数、最值、航行距离\n",
    "    speed_median = order_info_set['speed'].median()\n",
    "    speed_max = order_info_set['speed'].max()\n",
    "    speed_mean = order_info_set['speed'].mean()\n",
    "    latitude_median = order_info_set['latitude'].median()\n",
    "    longitude_median = order_info_set['longitude'].median()\n",
    "    total_dis = 0\n",
    "    for i in range(1, order_info_set.shape[0]):\n",
    "        total_dis = total_dis + haversine(order_info_set.iat[i-1,2],order_info_set.iat[i-1,3],\n",
    "                                          order_info_set.iat[i,2],order_info_set.iat[i,3])\n",
    "\n",
    "    feature_temp = pd.DataFrame({'loadingOrder':[order], 'speed_median':[speed_median],\n",
    "                                 'speed_max':[speed_max], 'speed_mean':[speed_mean],\n",
    "                                 'start_index':start_index, 'end_index':end_index,\n",
    "                                 'total_dis':[total_dis], 'label':[total_seconds],\n",
    "                                 'latitude_median':[latitude_median], 'longitude_median':[longitude_median],})\n",
    "\n",
    "    return feature_temp   \n",
    "\n",
    "def get_train_data(route_order_info, route,start_longitude,start_latitude,dest_longitude,dest_latitude):\n",
    "    order_list = route_order_info['loadingOrder'].unique()\n",
    "    print(route, order_list.shape)\n",
    "    \n",
    "    data_grouped = route_order_info.groupby('loadingOrder')\n",
    "\n",
    "    train_data = Parallel(n_jobs=8)(delayed(handle_train_data)\n",
    "                                    (order, group,start_longitude,start_latitude,dest_longitude,dest_latitude)\n",
    "                                    for order, group in data_grouped)\n",
    "#     for name, group in tqdm(data_grouped):\n",
    "#         handle_train_data(group,start_longitude,start_latitude,dest_longitude,dest_latitude)\n",
    "#         break\n",
    "    train_data = pd.concat(train_data)\n",
    "    return train_data.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/8 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CNSHK-CLVAP (93,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 12%|█▎        | 1/8 [01:05<07:37, 65.43s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      loadingOrder  speed_median  speed_max  speed_mean  start_index  \\\n",
      "0   AC118422359182          30.5         36       30.24            6   \n",
      "1   AC458999327091          32.0         34       29.62            0   \n",
      "2   AP254924452907          32.0         38       28.22            6   \n",
      "3   AS953078762857          26.0         41       22.64            0   \n",
      "4   BH140084651120          29.0         34       27.45            0   \n",
      "..             ...           ...        ...         ...          ...   \n",
      "88  YV465701899809          29.0         35       23.97         1259   \n",
      "89  YW391046018891          32.0         38       31.47            0   \n",
      "90  YW638325217174          36.0         37       31.60          391   \n",
      "91  ZM381066091094          28.0         33       27.33            0   \n",
      "92  ZU416619710839          28.0         33       27.33            0   \n",
      "\n",
      "    end_index     total_dis      label  latitude_median  longitude_median  \n",
      "0        4587  1.874406e+07  3674071.0       -42.861467       -111.103945  \n",
      "1       42349  1.935031e+07  2371475.0       -37.086800       -124.849404  \n",
      "2        6848  1.959391e+07  2436775.0       -37.558999       -102.474425  \n",
      "3       41969  2.307661e+07  3552989.0        30.299593        122.812347  \n",
      "4       15904  1.927490e+07  2453418.0       -37.633675        113.978956  \n",
      "..        ...           ...        ...              ...               ...  \n",
      "88       7468  1.943803e+07  2524813.0        -7.782894        130.041303  \n",
      "89       9311  1.930836e+07  2544312.0       -36.425142       -141.416147  \n",
      "90       7344  2.050437e+07  2389836.0       -15.886917       -113.884973  \n",
      "91       2689  1.890715e+07  2326887.0       -37.663814        -79.206938  \n",
      "92       2689  1.890715e+07  2326887.0       -37.663814        -79.206938  \n",
      "\n",
      "[93 rows x 10 columns]\n",
      "CNYTN-ARENA (84,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 25%|██▌       | 2/8 [02:15<06:40, 66.81s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      loadingOrder  speed_median  speed_max  speed_mean  start_index  \\\n",
      "0   AQ889642404042          30.0         35       26.07            0   \n",
      "1   AU510504184664          23.0        189       18.73            0   \n",
      "2   BK127471045315          23.0         39       19.23            0   \n",
      "3   BQ425517215925          24.0         40       19.75            0   \n",
      "4   BT552005629187          23.0         35       15.84          585   \n",
      "..             ...           ...        ...         ...          ...   \n",
      "79  YY314907990848          29.0         37       27.60            0   \n",
      "80  ZB984347982509          25.5         38       18.30            0   \n",
      "81  ZK570930733133          22.0         39       18.06            0   \n",
      "82  ZL336113063705          15.0         34       14.79          128   \n",
      "83  ZS104404854005          29.0         36       21.36            0   \n",
      "\n",
      "    end_index     total_dis      label  latitude_median  longitude_median  \n",
      "0       34997  2.090082e+07  3404148.0       -25.253358         29.246707  \n",
      "1        4747  2.040072e+07  3219329.0       -26.897588        -44.983609  \n",
      "2        6677  2.130163e+07  2912857.0       -24.243688         13.616962  \n",
      "3        6160  2.135192e+07  2812733.0       -24.539884         -9.221385  \n",
      "4       20953  2.150112e+07  3920461.0       -28.877540          6.032327  \n",
      "..        ...           ...        ...              ...               ...  \n",
      "79      37824  2.076932e+07  3079366.0       -26.722477         43.825480  \n",
      "80       7751  2.130632e+07  3117623.0       -24.221800        -25.164308  \n",
      "81       7627  2.126393e+07  2912857.0       -23.925519         24.863985  \n",
      "82       6904  2.054785e+07  3155158.0         1.271370        103.898745  \n",
      "83      12338  2.113976e+07  3235129.0         1.217846        100.960418  \n",
      "\n",
      "[84 rows x 10 columns]\n",
      "CNSHK-GRPIR (813,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 38%|███▊      | 3/8 [05:54<09:22, 112.56s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median  speed_max  speed_mean  start_index  \\\n",
      "0    AB900832754404          27.0         35       20.81           23   \n",
      "1    AC723925061448          22.0         35       19.68            0   \n",
      "2    AD205574515833          33.0         40       21.40           65   \n",
      "3    AD981352433721          30.0         36       22.07          135   \n",
      "4    AF547160379837          25.0         37       23.61            0   \n",
      "..              ...           ...        ...         ...          ...   \n",
      "808  ZY466646686415          21.0         35       17.29           76   \n",
      "809  ZZ580809707648          27.5         38       23.20            0   \n",
      "810  ZZ675551136571          27.5         36       19.94           19   \n",
      "811  ZZ676084526882          32.0         38       24.01            0   \n",
      "812  ZZ880516271852          23.5         35       18.22            0   \n",
      "\n",
      "     end_index     total_dis      label  latitude_median  longitude_median  \n",
      "0         2661  1.294682e+07  1650223.0        22.061016         46.605533  \n",
      "1         3601  1.352872e+07  1943697.0        31.238607         35.043078  \n",
      "2         3375  1.296588e+07  1861069.0        12.600921         59.692097  \n",
      "3         2697  1.297189e+07  1640721.0        12.336202         58.555500  \n",
      "4        15069  1.292133e+07  1584750.0        10.222463        101.251044  \n",
      "..         ...           ...        ...              ...               ...  \n",
      "808       4790  1.786400e+07  2193289.0        24.506996         56.813244  \n",
      "809       9743  1.294612e+07  1678473.0         5.698372        100.776336  \n",
      "810       7017  1.353313e+07  1996144.0        22.242191         52.273225  \n",
      "811       1939  1.303168e+07  1487690.0        13.293018         64.231103  \n",
      "812       1470  1.236394e+07  1702828.0         5.830238         89.989211  \n",
      "\n",
      "[813 rows x 10 columns]\n",
      "CNHKG-ARBUE (84,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 50%|█████     | 4/8 [06:58<06:31, 97.88s/it] "
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      loadingOrder  speed_median  speed_max  speed_mean  start_index  \\\n",
      "0   AQ889642404042          30.0         35       26.07            0   \n",
      "1   AU510504184664          21.5         39       17.00            0   \n",
      "2   BK127471045315          23.0         39       19.17            0   \n",
      "3   BQ425517215925          24.0         40       19.75            0   \n",
      "4   BT552005629187          23.0         35       15.84          585   \n",
      "..             ...           ...        ...         ...          ...   \n",
      "79  YY314907990848          29.0         37       27.60            0   \n",
      "80  ZB984347982509          25.5         38       18.42            0   \n",
      "81  ZK570930733133          21.0         39       18.05            0   \n",
      "82  ZL336113063705          15.0         34       14.79          128   \n",
      "83  ZS104404854005          29.0         36       21.37            0   \n",
      "\n",
      "    end_index     total_dis      label  latitude_median  longitude_median  \n",
      "0       34997  2.090082e+07  3404148.0       -25.253358         29.246707  \n",
      "1        4748  2.039572e+07  3219427.0       -26.897592        -44.997940  \n",
      "2        6678  2.130368e+07  2912967.0       -24.240995         13.598295  \n",
      "3        6160  2.135192e+07  2812733.0       -24.539884         -9.221385  \n",
      "4       20953  2.150112e+07  3920461.0       -28.877540          6.032327  \n",
      "..        ...           ...        ...              ...               ...  \n",
      "79      37824  2.076932e+07  3079366.0       -26.722477         43.825480  \n",
      "80       7753  2.130303e+07  3117826.0       -24.221659        -25.270358  \n",
      "81       7628  2.126496e+07  2912967.0       -23.925519         24.853172  \n",
      "82       6904  2.054785e+07  3155158.0         1.271370        103.898745  \n",
      "83      12339  2.114019e+07  3235250.0         1.217894        100.960246  \n",
      "\n",
      "[84 rows x 10 columns]\n",
      "CNNSA-GHTEM (297,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 62%|██████▎   | 5/8 [09:24<05:37, 112.43s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median  speed_max  speed_mean  start_index  \\\n",
      "0    AI556878934887          25.0         31       22.10            0   \n",
      "1    AM102991728889          23.0         31       20.20            0   \n",
      "2    AO618070615577          23.0         31       17.12            0   \n",
      "3    BC478944164976          27.0         32       22.69           28   \n",
      "4    BH268733506477          27.0         31       22.84            0   \n",
      "..              ...           ...        ...         ...          ...   \n",
      "292  ZN892068636040          27.0         34       19.96           21   \n",
      "293  ZP134592695325          21.5         32       17.81           24   \n",
      "294  ZV464815872671          25.0         32       22.22            0   \n",
      "295  ZY198608121827          18.0         31       18.07            6   \n",
      "296  ZY693697858676          28.0         34       27.79            0   \n",
      "\n",
      "     end_index     total_dis      label  latitude_median  longitude_median  \n",
      "0         4078  1.771745e+07  2635556.0        -5.349177         68.329310  \n",
      "1         3861  1.772671e+07  2752914.0       -16.856425         45.389983  \n",
      "2         1620  1.476818e+07  2778647.0         2.114501        103.842042  \n",
      "3         3867  1.810909e+07  2774417.0       -14.384014         40.421667  \n",
      "4         5072  1.753088e+07  2550937.0         1.189946        100.861847  \n",
      "..         ...           ...        ...              ...               ...  \n",
      "292       4895  1.807489e+07  2692643.0         1.256845         27.940288  \n",
      "293      25457  1.608854e+07  2806207.0         1.598677        104.438581  \n",
      "294       5405  1.758409e+07  2516346.0         1.259867        100.779545  \n",
      "295       5577  1.771797e+07  2842239.0         1.258256        103.544190  \n",
      "296      13643  1.687248e+07  2615984.0       -33.085689         22.783280  \n",
      "\n",
      "[297 rows x 10 columns]\n",
      "CNNSA-NAWVB (603,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 75%|███████▌  | 6/8 [14:08<05:27, 163.71s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       loadingOrder  speed_median  speed_max  speed_mean  start_index  \\\n",
      "0    AC971525935172          31.0         35       26.68            0   \n",
      "1    AE696573138143          27.0         31       21.75            0   \n",
      "2    AH212071198875          22.0         30       14.79            0   \n",
      "3    AI365973571487          28.0         34       23.70           46   \n",
      "4    AI556878934887          24.0         32       21.08            0   \n",
      "..              ...           ...        ...         ...          ...   \n",
      "598  ZV464815872671          24.0         30       20.99            0   \n",
      "599  ZX168584600462           0.0         37        3.46           60   \n",
      "600  ZY198608121827          18.0         30       17.87            6   \n",
      "601  ZY357155860431          27.0         31       21.66            0   \n",
      "602  ZY693697858676          28.0         34       27.94            0   \n",
      "\n",
      "     end_index     total_dis      label  latitude_median  longitude_median  \n",
      "0         2274  1.403036e+07  2042462.0       -21.867680         55.792325  \n",
      "1         3376  1.420590e+07  2042994.0        -7.355716         77.483566  \n",
      "2         4501  1.420937e+07  2476077.0       -25.252075         47.961383  \n",
      "3        11340  1.410811e+07  2149224.0        -9.629653         73.649947  \n",
      "4         3607  1.417350e+07  2194528.0         0.725611         89.306672  \n",
      "..         ...           ...        ...              ...               ...  \n",
      "598       4855  1.411178e+07  2094727.0         1.283539        101.214101  \n",
      "599     130594  1.434494e+07  2453598.0         1.252513        103.546160  \n",
      "600       4823  1.396044e+07  2334654.0         1.250961        103.560612  \n",
      "601       3109  1.417280e+07  2047355.0        -3.848006         81.493686  \n",
      "602      12666  1.310044e+07  2030161.0       -33.384720         23.520428  \n",
      "\n",
      "[603 rows x 10 columns]\n",
      "HKHKG-FRFOS (86,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 88%|████████▊ | 7/8 [14:43<02:05, 125.19s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      loadingOrder  speed_median  speed_max  speed_mean  start_index  \\\n",
      "0   AO393647206772          30.0         37       20.45            0   \n",
      "1   BS141811220707          13.5         40       17.09            0   \n",
      "2   BT719395802096          33.5         39       27.52            0   \n",
      "3   BY440962771393          28.0         37       20.01            0   \n",
      "4   BZ623190611735           0.0         38       13.46            0   \n",
      "..             ...           ...        ...         ...          ...   \n",
      "81  YT360665182781          21.0         42       19.34            0   \n",
      "82  ZB421092044630          28.5         39       24.20            0   \n",
      "83  ZE646628437331           7.5         38       14.27            0   \n",
      "84  ZI166705356011          25.0         37       21.37            0   \n",
      "85  ZM490479048723          30.0         37       21.19            0   \n",
      "\n",
      "    end_index     total_dis      label  latitude_median  longitude_median  \n",
      "0        3227  1.485986e+07  2589106.0        14.488346         48.049719  \n",
      "1        8438  1.523130e+07  2370221.0        37.955223         23.579902  \n",
      "2        6155  1.410652e+07  2086431.0        32.137416         30.571545  \n",
      "3        1096  1.351833e+07  1884790.0         1.434917        103.782167  \n",
      "4        3135  1.533596e+07  2209753.0        34.203525         21.877425  \n",
      "..        ...           ...        ...              ...               ...  \n",
      "81      12933  1.447931e+07  2393067.0        37.605196         23.678753  \n",
      "82      14341  1.610078e+07  2566265.0        22.472250         38.263891  \n",
      "83       2971  1.541483e+07  2176617.0        35.730961         14.856711  \n",
      "84      11384  1.553393e+07  2458544.0         6.357444         90.897747  \n",
      "85      12149  1.511110e+07  2470577.0        19.915538        101.494687  \n",
      "\n",
      "[86 rows x 10 columns]\n",
      "HONGKONG-BU (84,)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 8/8 [15:55<00:00, 119.45s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      loadingOrder  speed_median  speed_max  speed_mean  start_index  \\\n",
      "0   AQ889642404042          30.0         35       26.08            0   \n",
      "1   AU510504184664          23.0         40       17.32            0   \n",
      "2   BK127471045315          24.0         38       19.67            0   \n",
      "3   BQ425517215925          25.0         39       20.11            0   \n",
      "4   BT552005629187          22.0         32       15.63          612   \n",
      "..             ...           ...        ...         ...          ...   \n",
      "79  YY314907990848          29.0         37       27.62            0   \n",
      "80  ZB984347982509          24.0         38       18.22            0   \n",
      "81  ZK570930733133          21.0         38       18.43            0   \n",
      "82  ZL336113063705          15.0         34       14.87          128   \n",
      "83  ZS104404854005          28.5         36       21.23            0   \n",
      "\n",
      "    end_index     total_dis      label  latitude_median  longitude_median  \n",
      "0       35006  2.091529e+07  3407078.0       -25.223965         29.226185  \n",
      "1        4759  2.041206e+07  3221637.0       -26.897581        -45.213450  \n",
      "2        6700  2.126274e+07  2915606.0       -24.277513         12.906580  \n",
      "3        6175  2.136528e+07  2814764.0       -24.487245         -9.596947  \n",
      "4       20953  2.148767e+07  3918310.0       -28.928266          6.215666  \n",
      "..        ...           ...        ...              ...               ...  \n",
      "79      37830  2.078578e+07  3081717.0       -26.703365         43.818200  \n",
      "80       7770  2.134470e+07  3119866.0       -24.263284        -25.838212  \n",
      "81       7650  2.129281e+07  2915606.0       -23.925555         24.676477  \n",
      "82       6906  2.055818e+07  3157217.0         1.271306        103.898639  \n",
      "83      12357  2.114602e+07  3237399.0         1.218448        100.957105  \n",
      "\n",
      "[84 rows x 10 columns]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "for route in tqdm(test_order_belong_to_trace):\n",
    "    ports = route.split(\"-\")\n",
    "    start_port = ports[0]\n",
    "    dest_port = ports[-1]\n",
    "    start_longitude = port_data[start_port]['LONGITUDE']\n",
    "    start_latitude = port_data[start_port]['LATITUDE']\n",
    "    dest_longitude = port_data[dest_port]['LONGITUDE']\n",
    "    dest_latitude = port_data[dest_port]['LATITUDE']\n",
    "    route_order_info = get_train_route_order_data(route)\n",
    "    train_data = get_train_data(route_order_info, route,start_longitude,start_latitude,dest_longitude,dest_latitude)\n",
    "    train_data.to_csv(os.path.join(route_order_folder_path, \"{}_speed_dis_time.csv\".format(route)))\n",
    "    print(train_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.6 64-bit ('AI': conda)",
   "language": "python",
   "name": "python37664bitaiconda6859e03b37c34f0182c9bde8073269f7"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
